home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / ipdump.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  3KB  |  176 lines

  1. /* IP header tracing routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  /* Mods by PA0GRI */
  5. #include <stdio.h>
  6. #include "global.h"
  7. #include "config.h"
  8. #include "mbuf.h"
  9. #include "internet.h"
  10. #include "iface.h"
  11. #include "ip.h"
  12. #include "trace.h"
  13. #include "netuser.h"
  14.  
  15. void
  16. #ifdef MONITOR
  17. ip_dump(fp,bpp,check,mon)
  18. int mon;
  19. #else
  20. ip_dump(fp,bpp,check)
  21. #endif
  22. FILE *fp;
  23. struct mbuf **bpp;
  24. int check;
  25. {
  26.     struct ip ip;
  27.     int16 ip_len;
  28.     int16 length;
  29.     int16 csum;
  30.  
  31.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  32.         return;    
  33.  
  34. #ifdef MONITOR
  35.     if (!mon)
  36. #endif
  37.     fprintf(fp,"IP: ");
  38.     /* Sneak peek at IP header and find length */
  39.     ip_len = ((*bpp)->data[0] & 0xf) << 2;
  40.     if(ip_len < IPLEN){
  41.         fprintf(fp,"bad header\n");
  42.         return;
  43.     }
  44.     if(check)
  45.         csum = cksum(NULLHEADER,*bpp,ip_len);
  46.     else
  47.         csum = 0;
  48.  
  49.     ntohip(&ip,bpp);    /* Can't fail, we've already checked ihl */
  50.  
  51.     /* Trim data segment if necessary. */
  52.     length = ip.length - ip_len;    /* Length of data portion */
  53.     trim_mbuf(bpp,length);    
  54. #ifdef MONITOR
  55.     if (!mon)
  56. #endif
  57.     fprintf(fp,"len %u ",ip.length);
  58.     fprintf(fp,"%s",inet_ntoa(ip.source));
  59. #ifdef MONITOR
  60.     if (mon)
  61.         fprintf(fp, "->%s", inet_ntoa(ip.dest));
  62.     else
  63. #endif
  64.     fprintf(fp,"->%s ihl %u ttl %u",
  65.         inet_ntoa(ip.dest),ip_len,uchar(ip.ttl));
  66. #ifdef MONITOR
  67.     if (!mon)
  68. #endif
  69.     if(ip.tos != 0)
  70.         fprintf(fp," tos %u",uchar(ip.tos));
  71.     if(ip.offset != 0 || ip.flags.mf)
  72.         fprintf(fp," id %u offs %u",ip.id,ip.offset);
  73.     if(ip.flags.congest)
  74.         fprintf(fp," CE");
  75.     if(ip.flags.df)
  76.         fprintf(fp," DF");
  77.     if(ip.flags.mf){
  78.         fprintf(fp," MF");
  79.         check = 0;    /* Bypass host-level checksum verify */
  80.     }
  81.     if(csum != 0)
  82.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  83.  
  84.     if(ip.offset != 0){
  85.         fprintf(fp, "\n");
  86.         return;
  87.     }
  88.     switch(uchar(ip.protocol)){
  89.     case TCP_PTCL:
  90. #ifdef MONITOR
  91.         if (mon)
  92.             fprintf(fp, " TCP ");
  93.         else
  94. #endif
  95.         fprintf(fp," prot TCP\n");
  96. #ifdef MONITOR
  97.         tcp_dump(fp,bpp,ip.source,ip.dest,check,mon);
  98. #else
  99.         tcp_dump(fp,bpp,ip.source,ip.dest,check);
  100. #endif
  101.         break;
  102.     case UDP_PTCL:
  103. #ifdef MONITOR
  104.         if (mon)
  105.             fprintf(fp, " UDP ");
  106.         else
  107. #endif
  108.         fprintf(fp," prot UDP\n");
  109. #ifdef MONITOR
  110.         udp_dump(fp,bpp,ip.source,ip.dest,check,mon);
  111. #else
  112.         udp_dump(fp,bpp,ip.source,ip.dest,check);
  113. #endif
  114.         break;
  115.     case ICMP_PTCL:
  116. #ifdef MONITOR
  117.         if (mon)
  118.             fprintf(fp, " ICMP ");
  119.         else
  120. #endif
  121.         fprintf(fp," prot ICMP\n");
  122. #ifdef MONITOR
  123.         icmp_dump(fp,bpp,ip.source,ip.dest,check,mon);
  124. #else
  125.         icmp_dump(fp,bpp,ip.source,ip.dest,check);
  126. #endif
  127.         break;
  128.     case IP_PTCL:
  129. #ifdef MONITOR
  130.         if (mon)
  131.             fprintf(fp, " IP ");
  132.         else
  133. #endif
  134.         fprintf(fp," prot IP\n");
  135. #ifdef MONITOR
  136.         ip_dump(fp,bpp,check,mon);
  137. #else
  138.         ip_dump(fp,bpp,check);
  139. #endif
  140.         break;
  141. #ifdef AX25
  142.     case AX25_PTCL:
  143. #ifdef MONITOR
  144.         if (mon)
  145.             fprintf(fp, " AX25 ");
  146.         else
  147. #endif
  148.         fprintf(fp," prot AX25\n");
  149. #ifdef MONITOR
  150.         ax25_dump(fp,bpp,check,mon);
  151. #else
  152.         ax25_dump(fp,bpp,check);
  153. #endif
  154.         break;
  155. #endif
  156. #ifdef  RSPF
  157.     case RSPF_PTCL:
  158. #ifdef MONITOR
  159.         if (mon)
  160.             fprintf(fp, " RSPF\n");
  161.         else
  162. #endif
  163.         fprintf(fp," prot RSPF\n");
  164. #ifdef MONITOR
  165.         rspf_dump(fp,bpp,ip.source,ip.dest,check,mon);
  166. #else
  167.         rspf_dump(fp,bpp,ip.source,ip.dest,check);
  168. #endif
  169.         break;
  170. #endif
  171.     default:
  172.         fprintf(fp," prot %u\n",uchar(ip.protocol));
  173.         break;
  174.     }
  175. }
  176.